package com.kintreda.ieco_server.module.order.web;

import com.kintreda.ieco_server.bean.goods.Items;
import com.kintreda.ieco_server.core.web.BaseController;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.order.service.IRegenerantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.UUID;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.order.web
 * @Author ZhongYao_H
 * @Date 2019/5/3:20:10
 * @Year 2019 Month 05 Day 03
 * @Date_zh 2019 年 五月 星期五
 */
@Controller
@RequestMapping("/regenerant")
public class RegenerantController extends BaseController {

    @Autowired
    private IRegenerantService regenerantService;



    @RequestMapping("/test")
    public ModelAndView test(){
        return new ModelAndView("goods/goods-list");
    }

    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView andView = new ModelAndView("goods/goods-list");

        return andView;
    }

    @RequestMapping("/gettypes")
    @ResponseBody
    public Object getTypes(){
        return regenerantService.getType("1").toArray();
    }

    @ResponseBody
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Object query(String type){
        if (null == type){
            return regenerantService.query(" from Items ",null);
        }
        return regenerantService.query(" from Items where type = ?",new Object[]{type});
    }

    @ResponseBody
    @RequestMapping(value = "/queryC",method = RequestMethod.POST)
    public Object queryC(String type){
        if (null == type){
            return regenerantService.query(" from Items ",null);
        }
        return regenerantService.query(" from Items where tyepC = ?",new Object[]{type});
    }

    @RequestMapping("/addtype")
    public ModelAndView addType(){
        return new ModelAndView("goods/goods-add-type");
    }

    @RequestMapping(value = "/addtype",method = RequestMethod.POST)
    @ResponseBody
    public R addType(Items regenerant){

        if (regenerantService.isadd(regenerant.getName())){
            regenerant.setType(UUID.randomUUID().toString().replaceAll("-",""));
            return R.error("此类型已存在! 请勿重复添加");
        }
        regenerantService.save(regenerant);
        return R.ok("添加成功!");
    }

    @RequestMapping("/add")
    public ModelAndView add(){
        ModelAndView andView = new ModelAndView("goods/goods-add");
        List<Items> types = regenerantService.getType("1");
        andView.addObject("types",types);
        return andView;
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public R add(Items regenerant){
        Items items = regenerantService.get(regenerant.getType());
        regenerant.setTyepC(items.getTyepC());

        if (regenerantService.isadd(regenerant.getName())){
            return R.error("此名称物品已存在! 请勿添加重复物品");
        }
        regenerantService.save(regenerant);
        return R.ok("添加成功!").put("typeC",items.getTyepC());
    }



    @RequestMapping("/edit")
    public ModelAndView edit(String id){
        ModelAndView andView = new ModelAndView("goods/goods-add-edit");
        andView.addObject("id",id);
        andView.addObject("goods",regenerantService.get(id));
        return andView;
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Object edit(Items items,String Tid){
        Items ori = regenerantService.get(Tid);

        ori.setName(items.getName());
        ori.setIcon(items.getIcon());
        ori.setSorting(items.getSorting());
        ori.setRemark(items.getRemark());
        ori.setMoney(items.getMoney());
        regenerantService.update(ori);
        return R.ok("修改成功");
    }



    @ResponseBody
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public R del(String id){

        Items items = regenerantService.get(id);
        try {
            if (items.getTypeA().equals("1")){
                //删除类型 则删除类型下所有物品
                return R.error("删除类型将会对已有的数据造成影响,不能删除");
//                List<Items> query = regenerantService.query("from Items where type = ?", new Object[]{items.getId()});
//                regenerantService.deleteAll(query);
            }

        }catch (Exception e){
        }


        regenerantService.deleteByPK(id);
        return R.ok("删除成功!");
    }



}
